package com.itheima.service.Impl;

import com.itheima.common.entity.PageResult;

import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;

import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Iterator;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    /**
     * 新增检查套餐
     * @param setmeal
     */
    public void add(Setmeal setmeal) {
        setmealMapper.add(setmeal);
    }

    /**
     * 查询全部套餐
     * @param setmeal
     * @return
     */
    public List<Setmeal> findall(Setmeal setmeal) {
        return setmealMapper.findAll(setmeal);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }

    @Override
    public PageResult page(Integer currentPage, Integer pageSize) {
        //1.获取总记录数
        Long total=setmealMapper.count();
        //2.获取分页查询结果列表
        Integer start=(currentPage-1)*pageSize;
        List<Setmeal> list=setmealMapper.page(start,pageSize);

        //3.封装PageResult对象
        PageResult pageResult=new PageResult(total,list);
        return  pageResult;

    }

    @Override
    public void edit(Setmeal setmeal) {
        setmealMapper.edit(setmeal);
    }

    @Override
    public void editCheckGroups(List<String> idList,Integer id) {
        List<CheckGroup> idsBySetmealId = checkGroupMapper.findCheckGroupIdsBySetmealId(id);
        Iterator<CheckGroup> iterator = idsBySetmealId.iterator();
        while (iterator.hasNext()) {
            Integer id1 = iterator.next().getId();
            if (!idList.contains(id1.toString())) {
                // 这个元素被删掉了,删除库中对应的数据

            }else{
                idList.remove(id1+"");
            }
        }
        idList.forEach(System.out::println);
    }
}
